//
// BaseTimer.h
//
// $Id: //base/include/BaseTimer.h
//
// Library: base
// Package: BaseTimer
// Module:  BaseTimer
//
// Definition of the BaseTimer class.
//
// Copyright (c) 2012-2012, iGuard.
// doitsjz@163.com	jelin
//


#ifndef BASE_TIMER_INCLUDED
#define BASE_TIMER_INCLUDED

#include <pjlib.h>

#include "base.h"
#include "Proactor.h"

/**
	BaseTimer for timering every some time
*/

class Proactor;
class  BASE_API BaseTimer
{
public:
	BaseTimer(int id)
	{
        pj_memset(&timer_, 0, sizeof(timer_));
        timer_.user_data = this;
        timer_.cb = &timer_callback;
		timer_.id = id;
	}
	virtual ~BaseTimer(){};
public:
//	void SetUserData(void* user_data);
protected:
    //////////////////
    // Overridables
    //////////////////
    //
    // Timeout callback.
    //
    virtual void on_timeout(int id) 
    {
    }
protected:
    pj_timer_entry    timer_;
	friend class Proactor;
private:
	static void timer_callback( pj_timer_heap_t*,struct pj_timer_entry *entry)
    {
        BaseTimer *handler = (BaseTimer*)entry->user_data;

        handler->on_timeout(entry->id);
    }

};


#endif